import {
  getPage
} from '../../utils/config';
import wxApi from '../../utils/wxApi';
import api from '../../utils/api';
import {
  http,
  $loading,
  $toast
} from '../../utils/utils';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getPage: getPage,
    loading: 1,
    shopInfo: [],
    shopPage: 1,
    shopdone: false,
    shopabnor: false,
    loadmore: false,
    right: [{
      text: '删除',
    }, {
      text: '删除',
      style: 'color: #fa3e3e',
    }],
    selectAnli: [],
  },

  /**
   * 开启
   */
  onOpen(e) {
    this.toggleSwipe(e, true);
  },
  toggleSwipe(e, t) {
    const data = e.currentTarget.dataset || e.target.dataset;
    console.log(data);
    const {
      goodsInfo,
      shopInfo
    } = this.data;

    if (data.goodsId !== undefined) {
      goodsInfo[data.index].isOpen = t;
      this.setData({
        goodsInfo
      });
    } else if (data.id !== undefined) {
      shopInfo[data.index].isOpen = t;
      this.setData({
        shopInfo
      });
    }
  },

  // 获取案例
  getShopCollectionInfo(callback, a = false) {
    http.post(api.default, {
      data: {
        action: 'getcasesfor',
        shopid: this.data.shopid,
        productid:this.data.productid
      }
    }, a).then((res) => {
      const data = res.data.data;
      const shopInfo = data;
      let defSelect = shopInfo.filter(fi=>fi.c_num>0).map(mi=>mi.id+'')
      const pages = Math.ceil(data.shopcount / 10);
      let obj = { detail:defSelect }
      this.onChange(obj)


      //记录数据
      this.setData({
        shopPages: pages,
        shopabnor: pages == 0 ? true : false,
      });

      callback && callback(shopInfo);

      this.setData({
        shopdone: pages == 1 ? true : false,
      })
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      productid:options.id
    })

    //判断是否登录
    app.getLogin().then((res) => {
      this.setData({
        logged: res.code,
        userId: res.data.user.user_id,
        shopid: res.data.shop.ShopId,
        userRank: res.data.user.user_rank,
      });
      //获取全部案例
      this.getShopCollectionInfo((res) => {
        this.setData({
          shopInfo: res
        })
      });
    }).catch((res) => {
      this.setData({
        logged: res
      });
    });
  },

  onChange(event) {
    console.log('eventttttt',event)
    let ids = event.detail.join(',')
    this.setData({
      selectAnli: event.detail,
      ids: ids,
    });
  },

  handleguanlian(){
    let {ids,productid,selectAnli} = this.data;
    if(!selectAnli.length) return $toast('请选择关联案例')
    let params = {
      action: 'guanliancase',
      productid,
      ids
    }
    http.post(api.default, { data: params }).then(res=>{
      if (res.data.code == 0) {
        $toast('保存成功', {
          icon: 'success',
          times: 1000,
        }).then(()=>{
          wx.navigateBack({ delta: 1 });
        })
      }
    })
  },

  toggle(event) {
    console.log('toggleeeeeeeeeeeeee',event)
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  noop() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom() {

  //   const pages = this.data.shopPages;
  //   let page = this.data.shopPage;
  //   page++;
  //   if (pages >= page) {
  //     this.setData({
  //       shopPage: page,
  //       loadmore: true,
  //     });

  //     //拉取数据
  //     this.getShopCollectionInfo((res) => {
  //       const shopInfo = this.data.shopInfo;
  //       this.setData({
  //         shopInfo: shopInfo.concat(res),
  //         loadmore: false,
  //         shopdone: pages == page ? true : false,
  //       });
  //     }, false);
  //   }

  // },
})